<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 4/6/17
 * Time: 6:50 PM
 */

namespace common\models;

use common\components\bank\BankApi;
use yii\base\ErrorException;

/**
 * Class Bank
 * @package common\models
 *
 * @property BankGuaranteeCompanyBind[] $bankGuaranteeCompanyBinds
 * @property GuaranteeCompany[] $guaranteeCompanies
 * @property CreditOrder[] $creditOrders
 */
class Bank extends \common\models\base\Bank
{
    const STATUS_DEFAULT = 10;
    const STATUS_API_USE = 11;

    public static function getStatus()
    {
        return [
            self::STATUS_DEFAULT => '默认',
            self::STATUS_API_USE => 'Api使用',
        ];
    }
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getBankGuaranteeCompanyBinds()
	{
		return $this->hasMany(BankGuaranteeCompanyBind::className(), ['bank_id' => 'id']);
	}
	
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getGuaranteeCompanies()
	{
		return $this->hasMany(GuaranteeCompany::className(), ['id' => 'guarantee_company_id'])->viaTable('{{%bank_guarantee_company_bind}}', ['bank_id' => 'id']);
	}
	
	/**
	 * @return \yii\db\ActiveQuery
	 */
	public function getCreditOrders()
	{
		return $this->hasMany(CreditOrder::className(), ['bank_id' => 'id']);
	}

    /**
     * @param $name
     * @return null|static
     */
	public static function getByName($name)
    {
        $name = str_replace('（2017）', '', $name);
        $bank = static::findOne(['name' => $name]);
        if (!$bank){
            throw new ErrorException("没有找到银行");
        }
        return $bank;
    }

    /**
     * @param string $name
     * @return string|null $bank_sign_code
     * @throws ErrorException
     */
	public static function MapNameToCode($name)
    {
        $code = isset(self::$name_code_dist[$name])?self::$name_code_dist[$name]:null;
        return $code;
    }

    public static $name_code_dist = [
        '中国工商银行股份有限公司杭州延中支行' => 'icbc_zjfh',
        '（2017）中国工商银行股份有限公司杭州延中支行' => 'icbc_zjfh',
    ];
}